<!DOCTYPE html>
<?php
require 'common/sessioncheck.php';
?>
<?php
require 'common/mysqlCommon.php';  //将其他的php文件的代码引入当前页面
$frist=!count($_POST);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($frist){
            if(isset($_GET["id"])){
                $uid=(int)$_GET["id"];
                if($uid){
                        $sqlstring="select * from user_infos where uid=${uid}";
                        $result= getDataSetBySQL($sqlstring);
                        if(count($result)>0){
                            $row=$result[0];
                            ?>
                             <h1>用户编辑</h1>
                        <form action="edituser.php" method="post" enctype="multipart/form-data">
                            <input type="hidden" name="uid" 
                                   value="<?php echo $row["uid"];?>" />
                        用户名：<input type="text" name="username" 
                                   value="<?php echo $row["username"];?>"/><br/>
                            密 &nbsp;&nbsp;码：<input type="password" name="upwd"
                                   value="<?php echo $row["upwd"];?>"/><br/>
                            年 &nbsp;&nbsp;龄：
                            <select name="age">
                                <?php
                                for($a=10;$a<100;$a++){
                                 if($a==$row["age"]){
                                 echo "<option selected=\"selected\">${a}</option>"; 
                                 }else{
                                     echo "<option>${a}</option>"; 
                                 }
                                }
                                ?>

                            </select>
                            <br/>
                            性 &nbsp;&nbsp;别：
                            <select name="sex">
                                <option <?php echo $row["sex"]=='男'?"selected=\"selected\"":""; ?>>男</option>
                                <option <?php echo $row["sex"]=='女'?"selected=\"selected\"":""; ?>>女</option>
                                <option <?php echo $row["sex"]=='保密'?"selected=\"selected\"":""; ?>>保密</option>
                            </select>
                            <br/>
                            头像：<img src="<?php echo $row["pic"]; ?>" style="width:100px;height:100px" />
                             <br/>
                            更改头像：<input type="file" name="myimg"/>
                            <br/>
                            <input type="submit" value="确定"/>
                            <input type="reset" value="取消" />
                        </form>
                            <?php
                        }else{
                            echo "参数错误";
                        }
                   
                }else{
                    echo "参数错误";
                }
            }else{
                echo "参数错误";
            }
           
        }else{
            //页面处理数据
            $name=$_POST["username"];
            $pwd=$_POST["upwd"];
            $sex=$_POST["sex"];
            $age=$_POST["age"];
            $user_id=$_POST["uid"];
            
            $sqlstr="update user_infos set username='${name}',"
            . "upwd='${pwd}',sex='${sex}',age='${age}' where uid=".$user_id;
            if($_FILES["myimg"]["name"]==""){
                //没有选择新的头像
            }else{
                //设置上传文件的路径
                $newname="img/".time()."_".$_FILES["myimg"]["name"];
                move_uploaded_file($_FILES["myimg"]["tmp_name"],$newname );
                $sqlstr="update user_infos set username='${name}',"
            . "upwd='${pwd}',sex='${sex}',age='${age}', pic='${newname}' where uid=".$user_id;
            }
            
            $result= getResultBySQL($sqlstr);
            if($result){
                echo "<script>alert('编辑成功！');"
                    . "location='mysql001.php';</script>";
            }else{
                echo "<script>alert('添加失败,请重试!!');"
                    . "location='mysql001.php';</script>";
            }
        }
        ?>
        
        <?php
        // put your code here
        ?>
    </body>
</html>
